package com.bawei.module_mine.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.module_mine.R
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.InspectionOrderModelItem
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.NewsModelItem
import com.bawei.module_mine.model.RepairOrderModelItem
import com.bawei.module_mine.model.VisitOrderModelItem

class AuditCarAdapter : RecyclerView.Adapter<AuditCarAdapter.CarViewHolder>() {
    private val carAuditList= mutableListOf<CarOrderModelItem>()
    fun setCarData(data:List<CarOrderModelItem>){
        carAuditList.clear()
        carAuditList.addAll(data)
        notifyDataSetChanged()
    }
    override fun getItemViewType(position: Int): Int {
        return position
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CarViewHolder {
        return CarViewHolder(View.inflate(parent.context, R.layout.item_car_audit, null))
    }

    override fun getItemCount(): Int {
        return carAuditList.size
    }

    override fun onBindViewHolder(holder: CarViewHolder, position: Int) {
            val carOrder = carAuditList[position]
            when(carOrder.status){
                0 -> holder.carState.text="待审核"
                1 -> {
                    holder.carState.text="已通过"
                    holder.carYes.visibility = View.GONE
                    holder.carNo.visibility = View.GONE
                }
                -1 -> {
                    holder.carState.text="已拒绝"
                    holder.carYes.visibility = View.GONE
                    holder.carNo.visibility = View.GONE
                }
            }
//                 根据id设置颜色（使用 ContextCompat 兼容不同API）
            val colorRes = when (carOrder.status) {
                0 -> com.bawei.lib_common.R.color.color_yellow_dark
                1 -> com.bawei.lib_common.R.color.color_40b223
                -1 -> com.bawei.lib_common.R.color.color_examine_red
                else -> android.R.color.black // 默认颜色
            }
        when (carOrder.status) {
            0 -> holder.carState.setBackgroundResource(R.drawable.shape_item_repair_one)
            1 -> holder.carState.setBackgroundResource(R.drawable.shape_item_repair_there)
            -1 -> holder.carState.setBackgroundResource(R.drawable.shape_item_repair_four)
            else -> android.R.color.black // 默认颜色
        }
            holder.carState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
            holder.carName.text = "车位号: ${carOrder.userName} - ${carOrder.id}"
            holder.carTime.text = "转让期限: ${carOrder.createTime.substring(0,10)} 08:50:30"
            holder.carPlane.text = "小红 - ${carOrder.areaName}"
            holder.carAudit.text = "车位转让审核"
            holder.carPurpose.text = "转让原因: 临时外出"
    }
    class CarViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val carState: TextView = view.findViewById(R.id.item_audit_state)
        val  carAudit : TextView = view.findViewById(R.id.item_audit)
        val  carPlane: TextView = view.findViewById(R.id.item_audit_plane)
        val  carName: TextView = view.findViewById(R.id.item_audit_name)
        val  carPurpose: TextView = view.findViewById(R.id.item_audit_purpose)
        val  carTime: TextView = view.findViewById(R.id.item_audit_time)
        val  carYes: Button = view.findViewById(R.id.item_audit_yes)
        val  carNo: Button = view.findViewById(R.id.item_audit_no)
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = spacing,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.setItemViewCacheSize(20)
    }
}